package common

import (
	"errors"
	"fmt"
	Log "iot-base/auth/logger"
	"iot-base/common/ctrltype"
	"iot-base/common/notify"
	"time"
)

func CtrlResp(cmd *ctrltype.CtrlCmd) (map[string]interface{}, error) {
	var pResult *ctrltype.CtrlResult
	notify.Ctrl(cmd, 3*time.Second, func(pReply *ctrltype.CtrlResult) {
		pResult = pReply

	}, true)
	if pResult == nil {
		return nil, nil
	}
	if pResult.Error != nil && pResult.Error.Error() == "time out" {
		fmt.Println("CtrlResp", "cmd", cmd, "result", pResult.Error)
		Log.Log.Error("CtrlResp", "cmd", cmd, "result", pResult.Error)
		return nil, errors.New("time out")
	}
	if pResult.Error != nil {
		fmt.Println("CtrlResp", "cmd", cmd, "result", pResult.Error)
		Log.Log.Error("CtrlResp", "cmd", cmd, "result", pResult.Error)
		return nil, pResult.Error
	}
	return pResult.Result, nil
}
